﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for ExplorerFrame.xaml
    /// </summary>
    public partial class ExplorerFrame : UserControl
    {
        #region Private Variables
        private RootInfoViewSource rootSource = null;
        #endregion

        #region Construction/Initialization
        public ExplorerFrame()
        {
            InitializeComponent();
        }

        public ExplorerFrame(RootInfoViewSource source)
        {
            rootSource = source;
            DataContext = source;

            InitializeComponent();

            Rebuild();
        }

        public void SetSource(RootInfoViewSource source)
        {
            rootSource = source;
            DataContext = source;

            Rebuild();
        }
        #endregion

        private void Rebuild()
        {
            if (rootSource.IsDummy)
                tvSources.ItemsSource = rootSource.NestedSources;
            else
                tvSources.ItemsSource = new System.Collections.ArrayList() { rootSource };

            tvSources_SelectedItemChanged(null, null);
        }

        private void tvSources_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            InfoViewSource src = null;
            if (tvSources.SelectedItem != null)
            {
                src = (InfoViewSource)tvSources.SelectedItem;
                if (src.IsDummy)
                    src = null;
            }
            sourceFrame.DataContext = src;
            if (src != null)
            {
                var settings = rootSource.VisualSettings;
                settings.SetValue(Settings.Manager.LAST_SELECTED_SOURCE_KEY, src.Path);
            }
        }

    }
}
